{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fe35686d",
   "metadata": {},
   "source": [
    "第一步：加载数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c4b88b3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data_file = '../03_dataset/item2/item2-ss-data.txt'\n",
    "raw_df = pd.read_csv(data_file, sep='\\\\s+', header=None, skiprows=22)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd415455",
   "metadata": {},
   "source": [
    "第二步：提取数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98a9785e",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_array = raw_df.values\n",
    "x = np.hstack([raw_array[::2,:], raw_array[1::2,:2]])\n",
    "y = raw_array[1::2,2]\n",
    "print(f\"特征形状: {x.shape}\") # 应该是 (506, 13)\n",
    "print(f\"标签形状: {y.shape}\") # 应该是 (506,)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7a2cd81",
   "metadata": {},
   "source": [
    "第三步：分割数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b08a0f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1)\n",
    "print(f\"训练集特征形状: {x_train.shape}\") # 应该是 (354, 13)\n",
    "print(f\"测试集特征形状: {x_test.shape}\") # 应该是 (152, 13)\n",
    "print(f\"训练集标签形状: {y_train.shape}\") # 应该是 (354,)\n",
    "print(f\"测试集标签形状: {y_test.shape}\") # 应该是 (152,)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdcfeb10",
   "metadata": {},
   "source": [
    "第四步：初始化一个字典列表，用来存储三种模型的训练结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6b20ce29",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression, Ridge, Lasso\n",
    "model_list = [\n",
    "    {\n",
    "        'model': LinearRegression(),\n",
    "        'name': '线性回归',\n",
    "        'scores': []\n",
    "    },\n",
    "    {\n",
    "        'model': Ridge(),\n",
    "        'name': '岭回归', \n",
    "        'scores': []\n",
    "    },\n",
    "    {\n",
    "        'model': Lasso(),\n",
    "        'name': '套索回归',\n",
    "        'scores': []\n",
    "    }\n",
    "]\n",
    "\n",
    "alphas = [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1,5, 10, 50]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded385c1",
   "metadata": {},
   "source": [
    "第五步：线性回归模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3595b20",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "model_item = model_list[0] #线性回归\n",
    "\n",
    "for alpha in alphas:\n",
    "    model_item['model'].fit(x_train, y_train)\n",
    "    score = model_item['model'].score(x_test, y_test)\n",
    "    model_item['scores'].append(score)\n",
    "\n",
    "print(f\"线性回归模型的预测准确率为：{model_item['scores'][0]:.5f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b955cda0",
   "metadata": {},
   "source": [
    "第六步：岭回归模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5c4b968",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_item = model_list[1] #岭回归\n",
    "\n",
    "for alpha in alphas:\n",
    "    model_item['model'].alpha = alpha\n",
    "    model_item['model'].fit(x_train, y_train)\n",
    "    score = model_item['model'].score(x_test, y_test)\n",
    "    model_item['scores'].append(score)\n",
    "    \n",
    "print(f\"岭回归模型在不同alpha值下的预测准确率为：{model_item['scores']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e94c258e",
   "metadata": {},
   "source": [
    "第七步：套索回归模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18a21d3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_item = model_list[2] #套索回归\n",
    "\n",
    "for alpha in alphas:\n",
    "    model_item['model'].alpha = alpha\n",
    "    model_item['model'].fit(x_train, y_train)\n",
    "    score = model_item['model'].score(x_test, y_test)\n",
    "    model_item['scores'].append(score)\n",
    "    \n",
    "print(f\"套索回归模型在不同alpha值下的预测准确率为：{model_item['scores']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "493482bf",
   "metadata": {},
   "source": [
    "第八步：绘制画板，对比三种模型的预测准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fe269ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif']='Simhei'\n",
    "fig = plt.figure(figsize=(10,7))\n",
    "for i, model_item in enumerate(model_list):\n",
    "    plt.subplot(2, 2, i+1)\n",
    "    plt.plot(range(len(alphas)), model_item['scores'], 'g-')\n",
    "    plt.title(model_item['name'])\n",
    "    score_max = max(model_item['scores'])\n",
    "    print(f\"{model_item['name']}模型的最大预测准确率为：{score_max}\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml-course",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
